'use strict';//用户增删改查
const Controller = require('egg').Controller;
const utility = require("utility");
// const moment = require("moment");
class UsersController extends Controller {
    async index() { //模糊搜索跟分页查询
        const { ctx, app } = this;
        const { keyname, page = 1, number = 5 } = ctx.request.query
        const offset = (page - 1) * number
        let data, count
        if (keyname && page) {
            data = await app.mysql.query(`select id,user_name,password,sex,email,Telephone,head,name,my_comments from users where user_name like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from users where user_name like '%${keyname}%'`)
        } else if (keyname) {
            data = await app.mysql.query(`select id,user_name,password,sex,email,Telephone,head,name,my_comments from users where user_name ='%${keyname}%' or user_name  like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from users where user_name like '%${keyname}%' or user_name like '%${keyname}%'`)
        } else {
            data = await app.mysql.query(`select id,user_name,password,sex,email,Telephone,head,name,my_comments from users limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from users`)
        }
        ctx.body = {
            code: 0,
            msg: "查找成功",
            data,
            count: count
        }
    }
    async create() {
        const { ctx, app } = this;
        const { user_name, password, sex, Telephone, email, head,name, time_d,my_comments } = ctx.request.body;
        const result = await app.mysql.insert('users', { user_name, password: utility.md5(password), sex, email, Telephone, head,name,my_comments,  time_d:Date.now() })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "添加成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 406,
                msg: "添加失败"
            };
        }
    }
    async update() {
        const { ctx, app } = this;
        const {  password, sex, Telephone, email, head,name } = ctx.request.body;
        const { id } = ctx.params;
        const row = {
            password,
            sex,
            email,
            Telephone,
            head,
            name   
        }
        const options = {
            where: {
                id: +id
            }
        };
        const result = await app.mysql.update('users', row, options)
        if ( result ) {
            ctx.body = {
                code: 0,
                msg: "修改成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 408,
                msg: "修改失败",
            };
        }
    }


    async destroy() {
        const { ctx, app } = this;
        const { id } = ctx.params;
        const result = await app.mysql.delete(`users`, { id: +id })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "删除成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 407,
                msg: "删除失败"
            };
        }
    }
}
module.exports = UsersController;
